'use strict'
// axios 封装的工具类
import axios from 'axios'
import qs from 'qs'
//import { Indicator } from 'mint-ui';

//axios.defaults.baseURL = 'http://localhost:8083/';
if (process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = "http://192.168.13.78:3000/"
  //axios.defaults.baseURL = 'https://www.easy-mock.com/mock/5a4d85d3e5f9cd651a7fbe77/test/';
  //axios.defaults.baseURL = 'http://192.168.13.78:8083/';
} else if (process.env.NODE_ENV == 'debug') {
  //axios.defaults.baseURL = 'https://www.easy-mock.com/mock/5a4d85d3e5f9cd651a7fbe77/test/';
} else if (process.env.NODE_ENV == 'production') {
  axios.defaults.baseURL = 'http://192.168.13.78:3000/';
}

axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.responseType = 'json';

//axios.defaults.baseURL = 'api/';
axios.interceptors.request.use(config => {
  // loading
  //配置config
  config.headers.Accept = 'application/json';
  // Indicator.open({
  // 	text: '加载中...',
  // 	spinnerType: 'fading-circle'
  // });
  return config;
}, error => {
  //隐藏图标
  //Indicator.close();
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  //隐藏图标
  //Indicator.close();
  return response
}, error => {
  //隐藏图标
  //Indicator.close();
  return Promise.resolve(error.response)
})
const api = {
  async get(url, data) {
    try {
      let res = await axios.get(url, {
        params: data
      })
      res = res.data
      return new Promise((resolve) => {
        if (res.code === 0) {
          resolve(res)
        } else {
          resolve(res)
        }
      })
    } catch (err) {
      console.log(err)
    }
  },
  async post(url, data) {
    try {
      let res = await axios.post(url, data);
      //let res = await axios.post(url, qs.stringify(data))
      res = res.data
      //console.log(res);
      return new Promise((resolve, reject) => {
        // if (res.code === 200) {
        //   resolve(res)
        // } else {
        //   reject(res)
        // }
        resolve(res)
      })
    } catch (err) {
      reject(err)
    }
  },
}
export {
  api
}
